using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grappling_Rope : MonoBehaviour
{
    [Header("一般引用:")]
    public Perfecter_Grapple grapplingGun; //抓钩枪
    public LineRenderer m_lineRenderer; //线渲染器

    [Header("一般设置:")]
    [SerializeField] private int percision = 40; //精度
    [Range(0, 20)] [SerializeField] private float straightenLineSpeed = 5; //直线速度

    [Header("绳索动画设置:")]
    public AnimationCurve ropeAnimationCurve; //绳索动画曲线
    [Range(0.01f, 4)] [SerializeField] private float StartWaveSize = 2; //起始波动大小
    float waveSize = 0; //波动大小

    [Header("绳索进度:")]
    public AnimationCurve ropeProgressionCurve; //绳索进度曲线
    [SerializeField] [Range(1, 50)] private float ropeProgressionSpeed = 1; //绳索进度速度

    float moveTime = 0; //移动时间

    [HideInInspector] public bool isGrappling = true; //是否正在抓取

    bool strightLine = true; //是否为直线

    private void OnEnable() //启用时执行
    {
        moveTime = 0;
        m_lineRenderer.positionCount = percision;
        waveSize = StartWaveSize;
        strightLine = false;

        LinePointsToFirePoint(); //线点对准发射点

        m_lineRenderer.enabled = true;
    }

    private void OnDisable() //禁用时执行
    {
        m_lineRenderer.enabled = false;
        isGrappling = false;
    }

    private void LinePointsToFirePoint() //线点对准发射点
    {
        for (int i = 0; i < percision; i++)
        {
            m_lineRenderer.SetPosition(i, grapplingGun.firePoint.position); //绘制连接抓取点和抓钩枪位置的绳子
        }
    }

    private void Update() //更新函数
    {
        moveTime += Time.deltaTime;
        DrawRope(); //绘制绳索
    }

    void DrawRope() //绘制绳索
    {
        if (!strightLine)
        {
            if (m_lineRenderer.GetPosition(percision - 1).x == grapplingGun.grapplePoint.x)
            {
                strightLine = true;
            }
            else
            {
                DrawRopeWaves(); //绘制绳索波动
            }
        }
        else
        {
            if (!isGrappling)
            {
                grapplingGun.Grapple(); //抓取
                isGrappling = true;
            }
            if (waveSize > 0)
            {
                waveSize -= Time.deltaTime * straightenLineSpeed;
                DrawRopeWaves(); //绘制绳索波动
            }
            else
            {
                waveSize = 0;

                if (m_lineRenderer.positionCount != 2) { m_lineRenderer.positionCount = 2; }

                DrawRopeNoWaves(); //绘制无波动的绳索
            }
        }
    }

    void DrawRopeWaves() //绘制绳索波动
    {
        for (int i = 0; i < percision; i++)
        {
            float delta = (float)i / ((float)percision - 1f);
            Vector2 offset = Vector2.Perpendicular(grapplingGun.grappleDistanceVector).normalized * ropeAnimationCurve.Evaluate(delta) * waveSize; //计算偏移量
            Vector2 targetPosition = Vector2.Lerp(grapplingGun.firePoint.position, grapplingGun.grapplePoint, delta) + offset; //目标位置
            Vector2 currentPosition = Vector2.Lerp(grapplingGun.firePoint.position, targetPosition, ropeProgressionCurve.Evaluate(moveTime) * ropeProgressionSpeed); //当前位置

            m_lineRenderer.SetPosition(i, currentPosition); //设置线的位置
        }
    }

    void DrawRopeNoWaves() //绘制无波动的绳索
    {
        m_lineRenderer.SetPosition(0, grapplingGun.firePoint.position); //设置线的起始位置
        m_lineRenderer.SetPosition(1, grapplingGun.grapplePoint); //设置线的结束位置
    }
}
